<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/20
 * Time: 14:55
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Club extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $club_name = trim(input('club_name'), ' ');
            $where['club_status'] = 1;
            if(isset($club_name) && $club_name != ''){
                $where['club_name'] = ['like', '%' . $club_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('club')->where($where)->count();// 获取总条数
            $lists = Db::name('club')
                ->field('club_id, club_name, club_time, club_intro, club_image')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('club_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加俱乐部
     * @return mixed|\think\response\Json
     */
    public function clubAdd(){
        if(Request::instance()->isPost()){
            $club_name = trim(input('club_name'), ' ');
            $club_image = input('club_image');
            $club_intro = input('club_intro');
            $insert = [
                'club_name' => $club_name,
                'club_intro' => $club_intro,
                'club_image' => $club_image,
                'club_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_club')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('club/clubadd');
    }
    
    /**
     * 编辑俱乐部
     * @return mixed|\think\response\Json
     */
    public function clubEdit(){
        if(Request::instance()->isPost()){
            $club_name = trim(input('club_name'), ' ');
            $club_image = input('club_image');
            $club_intro = trim(input('club_intro'), ' ');
            $id = input('club_id');
            $update = [
                'club_name' => $club_name,
                'club_intro' => $club_intro,
                'club_updatetime' => date('Y-m-d H:i:s')
            ];
            if(!empty($club_image)){
                $update['club_image'] = $club_image;
            }
            $res = Db::table('up_club')
                ->where('club_id', $id)
                ->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $club_id = input('club_id');
        $club = Db::table('up_club')
            ->where('club_id', $club_id)
            ->field('club_name, club_intro, club_image')
            ->find();
        $this->assign([
            'club_id' => $club_id,
            'club' => $club
        ]);
        return $this->fetch('club/clubedit');
    }
    
    /**
     * 删除俱乐部
     * @return \think\response\Json
     */
    public function clubDel(){
        $club_id = input('id');
        $res = Db::table('up_club')->where('club_id', $club_id)->setField('club_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}